﻿/**   
 * @Title: SportteryFootballChoosePage.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.sportteryfootballapg 
 * @Description: (竞彩足球选择页面) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-8-15 上午10:24:39  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.sportteryfootballapg;

/** 
 * @ClassName: SportteryFootballChoosePage 
 * @Description: 精彩足球选择页面) 
 * @author: City Jiangsy@500wan.com
 * @date: 2011-8-15 上午10:24:39 
 * @update by liugan 2012-3-13 下午 19:05
 *  
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.model.sportteryfootball.SportteryFootballAgainstInfo;
import com.esun.model.sportteryfootball.SportteryFootballLotteryInfo;
import com.esun.rabbit2.communicaters.sportteryfootball.SportteryFootballGetTeamListCommunicater;
import com.esun.rabbit2.communicaters.sportteryfootball.SportteryFootballNoFilterAgainstInfoCommunicater;
import com.esun.rabbit2.communicaters.sportteryfootball.SportteryFootballRecommendAgainstInfoCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBuyLottery;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseActivity;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseList;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryFootballAgp;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.img.ImageListCache;
import com.esun.util.log.EDebug;

public class SportteryFootballChooseList extends SportteryChooseList implements OnClickListener, OnScrollListener {

	private Context context;
	private View view;
	private SportteryBuyLottery buyLotteryPage;

	private Button btnClaen;
	private Button btnSubmit;
	private Button btnRefresh;
	private ArrayList<Hashtable<String, Object>> listContent;
	private ArrayList<SportteryFootballAgainstInfo> listInfo;
	private TeamListAdapter teamListAdapter;
	private GridView gridSaishiList;
	private RadioGroup rgTop;

	private ExpandableListView expandableListViewContent;
	private SportteryFootballExpandableListAdapter sportteryFootballExpandableListAdapter;

	private int type;

	private LinearLayout linearTitle;
	private TextView tvDate;
	private TextView tvDay;
	private TextView tvSaishiCount;
	private ImageView imgExpand;

	private ArrayList<Boolean> saishiItemCheckedList;
	private SportteryFootballGetTeamListCommunicater sportteryFootballGetTeamListCommunicater = new SportteryFootballGetTeamListCommunicater();;
	private SportteryFootballNoFilterAgainstInfoCommunicater sportteryFootballNoFilterAgainstInfoCommunicater;
	private SportteryFootballRecommendAgainstInfoCommunicater sportteryFootballRecommendAgainstInfoCommunicater;
	public static Handler handlerChoosePage;

	private boolean isGettingTeamList = false;
	private boolean isUpdateAgainstList = false;
	private boolean isRecommend = false;
	private ArrayList<Object[]> listImageCache = new ArrayList<Object[]>();

	private RelativeLayout progressdialogRefresh;

	private ImageListCache imageListCache = null;

	private ArrayList<View> alViewsList = new ArrayList<View>();

	// private int indicatorGroupId = -1;

	/** 2011-09-06 byCity 本地筛选工具 */
	AgainstTeamListLocalFilter againstTeamListLocalFilter;

	public SportteryFootballChooseList(Context context, View view) {
		this.view = view;
		this.context = context;

		handlerInit();
		init();
		refresh();
	}

	private void handlerInit() {
		handlerChoosePage = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case GETTEAMLIST_SUCCEED:
					isGettingTeamList = false;
					sportteryFootballGetTeamListCommunicater.updateTeamInfo();
					EDebug.o(SportteryFootballChooseList.this, "获取联赛列表成功");
					Message msgToGroup = SportteryFootballAgp.handlerSportteryFootballAgp.obtainMessage(SportteryFootballAgp.NO_FILTER, null);
					SportteryFootballAgp.handlerSportteryFootballAgp.sendMessage(msgToGroup);
					updateDialogFilter();
					break;

				case GETAGAINSTLIST_SUCCEED:
					isUpdateAgainstList = false;
					getAgainstSucceed();
					break;

				case GETRECOMMENDAGAINSTLIST_SUCCEED:
					isUpdateAgainstList = false;
					getRecommendAgainstSucceed();
					break;

				case DO_REFRESH:
					refresh();
					break;

				case DO_FINISH:
					((Activity) context).getParent().finish();
					break;

				case DO_CLEAR:
					cleanListView();
					break;

				case DO_BACK_UPDATE:
					listContent.clear();
					listContent.addAll(SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getLotterList());
					expandableListProcesForPageSwich();
					break;

				case GETAGAINSTLIST_FAIL:

					isUpdateAgainstList = false;
					sportteryFootballExpandableListAdapter.notifyDataSetChanged();
					linearTitle.setVisibility(View.INVISIBLE);
					progressdialogRefresh.setVisibility(View.INVISIBLE);
					Toast.makeText(context, sportteryFootballNoFilterAgainstInfoCommunicater.getServerMsg(), Toast.LENGTH_SHORT).show();
					break;

				case GETRECOMMENDAGAINSTLIST_FAIL:

					isUpdateAgainstList = false;
					listGroup.clear();
					listChild.clear();
					sportteryFootballExpandableListAdapter.notifyDataSetChanged();
					linearTitle.setVisibility(View.INVISIBLE);
					progressdialogRefresh.setVisibility(View.INVISIBLE);
					Toast.makeText(context, sportteryFootballRecommendAgainstInfoCommunicater.getServerMsg(), Toast.LENGTH_SHORT).show();
					break;

				case GETTEAMLIST_FAIL:

					isGettingTeamList = false;
					progressdialogRefresh.setVisibility(View.INVISIBLE);
					Toast.makeText(context, sportteryFootballGetTeamListCommunicater.getServerMsg(), Toast.LENGTH_SHORT).show();
					break;

				case Config.CONNECTION_FAIL:

					sportteryFootballExpandableListAdapter.notifyDataSetChanged();
					linearTitle.setVisibility(View.INVISIBLE);
					isUpdateAgainstList = false;
					progressdialogRefresh.setVisibility(View.INVISIBLE);
					Toast.makeText(context, Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();
					break;

				case GETTEAMLIST_CONNECTION_FAIL:

					isGettingTeamList = false;
					progressdialogRefresh.setVisibility(View.INVISIBLE);
					Toast.makeText(context, Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();
					break;

				case DO_FILTER:

					showDialogFilter();
					break;

				case GET_IMAGECACHE_SUCCEED:

					// pdTest.cancel();

					if (imageListCache == null) {
						EDebug.o(SportteryFootballChooseList.this, "加载触发,走你");
						EDebug.o(SportteryFootballChooseList.class, " creat object !!" + listImageCache.size());
						imageListCache = new ImageListCache(context, listImageCache);
					} else {
						EDebug.o(SportteryFootballChooseList.this, "重加载触发,走你");
						imageListCache.addToLine(listImageCache);
					}

					if (scrollViewFilter != null) {
						EDebug.o(SportteryFootballChooseList.this, "线程触发,scrollto 0");
						scrollViewFilter.scrollTo(0, 0);
					}
					break;

				default:
					break;
				}
			}

		};
	}

	private void updateDialogFilter() {
		// boolean 列表,存放checkbox状态
		saishiItemCheckedList.clear();
		// String 选中的联赛名称
		listSelectedLeagueNmae.clear();
		// View 缓存adapter中的view
		alViewsList.clear();
		// object[] 调用图片加载
		listImageCache.clear();
		if (imageListCache != null) {
			imageListCache = null;
		}
		for (int i = 0; i < SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().size(); i++) {
			saishiItemCheckedList.add((Boolean) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(i).get(IS_CHECKED));
			listSelectedLeagueNmae.add((String) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(i).get(SAISHI_NAME));
		}
	}

	private void getAgainstSucceed() {
		try {
			progressdialogRefresh.setVisibility(View.INVISIBLE);
		} catch (Exception e) {
			EDebug.o(SportteryFootballChooseList.this, e.toString());
		}
		listInfo.clear();
		listInfo.addAll(sportteryFootballNoFilterAgainstInfoCommunicater.getAgainstInfoList());
		sortListInfo();
		updateListContent();
		updateListExpandadble(listContent, true);
		sportteryFootballExpandableListAdapter.notifyDataSetChanged();

		updateTitleView(0);
		for (int i = 0; i < listGroup.size(); i++)
			expandableListViewContent.expandGroup(i);
	}

	private void getRecommendAgainstSucceed() {
		progressdialogRefresh.setVisibility(View.INVISIBLE);

		listInfo.clear();
		listInfo.addAll(sportteryFootballRecommendAgainstInfoCommunicater.getAgainstInfoList());
		sortListInfo();
		updateListContent();
		updateListExpandadble(listContent, true);
		sportteryFootballExpandableListAdapter.notifyDataSetChanged();

		updateTitleView(0);
		for (int i = 0; i < listGroup.size(); i++)
			expandableListViewContent.expandGroup(i);
	}

	public View getView() {
		((SportteryChooseActivity) context).setCurrentPageFlag(0);
		Message msg = SportteryFootballAgp.handlerSportteryFootballAgp.obtainMessage(SportteryFootballAgp.IN_CHOOSE_VIEW, null);
		SportteryFootballAgp.handlerSportteryFootballAgp.sendMessage(msg);
		return view;
	}

	private void refresh() {
		if (canCommunicate()) {
			progressdialogRefresh.setVisibility(View.VISIBLE);
			type = 2;
			rangqiuType = 0;
			peilvType = 0;
			if (isRecommend) {
				threadRecommendAgainst();
			} else {
				threadAgainst();
			}
			threadGetTeamList();
		} else {
			EDebug.o(SportteryFootballChooseList.this, " 通信中..");
		}
	}

	private void init() {

		btnClaen = (Button) view.findViewById(R.id.sportteryfootballchooseact_btn_clean);
		btnSubmit = (Button) view.findViewById(R.id.sportteryfootballchooseact_btn_submit);
		btnRefresh = (Button) view.findViewById(R.id.sportteryfootballchooseact_btn_refresh);

		btnClaen.setOnClickListener(this);
		btnSubmit.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);

		listContent = new ArrayList<Hashtable<String, Object>>();
		listInfo = new ArrayList<SportteryFootballAgainstInfo>();

		sportteryFootballNoFilterAgainstInfoCommunicater = new SportteryFootballNoFilterAgainstInfoCommunicater();
		sportteryFootballRecommendAgainstInfoCommunicater = new SportteryFootballRecommendAgainstInfoCommunicater();

		expandableListViewContent = (ExpandableListView) view.findViewById(R.id.sportteryfootballchooseact_expandalblelistview_content);
		sportteryFootballExpandableListAdapter = new SportteryFootballExpandableListAdapter(context);
		expandableListViewContent.setAdapter(sportteryFootballExpandableListAdapter);
		expandableListViewContent.setOnScrollListener(this);

		for (int i = 0; i < listInfo.size(); i++) {
			EDebug.o(SportteryFootballChooseList.this, listInfo.get(i).getMatchNumDate());
		}
		for (int i = 0; i < listContent.size(); i++) {
			EDebug.o(SportteryFootballChooseList.this, "is title = " + listContent.get(i).get(IS_TITLE));
		}
		// 赛事选择列表初始化
		saishiItemCheckedList = new ArrayList<Boolean>();

		linearTitle = (LinearLayout) view.findViewById(R.id.sportteryfootballchooseact_linear_title);
		tvDate = (TextView) view.findViewById(R.id.sportteryfootballchooseact_tv_date);
		tvDay = (TextView) view.findViewById(R.id.sportteryfootballchooseact_tv_day);
		tvSaishiCount = (TextView) view.findViewById(R.id.sportteryfootballchooseact_tv_mtachCount);
		imgExpand = (ImageView) view.findViewById(R.id.sportteryfootballchooseact_img_expand);
		linearTitle.setVisibility(View.GONE);

		progressdialogRefresh = (RelativeLayout) view.findViewById(R.id.sportteryfootball_relative_progress);
		progressdialogRefresh.setOnClickListener(this);
		SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getAllLeagueList().clear();
		SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getRecommendLeagueList().clear();
		initRadioGroupTop();
	}

	private void initRadioGroupTop() {
		rgTop = (RadioGroup) view.findViewById(R.id.sportteryfootballchooseact_rg_top);
		rgTop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.sportteryfootballchooseact_rb_all:
					isRecommend = false;
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getRecommendLeagueList().clear();
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getRecommendLeagueList().addAll(listContent);
					listContent.clear();
					if (SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getAllLeagueList().isEmpty()) {
						listGroup.clear();
						listChild.clear();
						refresh();
					} else {
						listContent.addAll(SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getAllLeagueList());
						expandableListProcesForPageSwich();
					}
					break;

				case R.id.sportteryfootballchooseact_rb_recommend:
					isRecommend = true;
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getAllLeagueList().clear();
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getAllLeagueList().addAll(listContent);
					listContent.clear();
					if (SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getRecommendLeagueList().isEmpty()) {
						listGroup.clear();
						listChild.clear();
						refresh();
					} else {
						listContent.addAll(SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getRecommendLeagueList());
						expandableListProcesForPageSwich();
					}
					break;

				default:
					break;
				}
			}
		});
	}

	/**
	 * 页面切换时expandablelistview的处理, 筛选显示, adapter加载, scrollview滚至最上, 更新titlebar显示, 展开每一条group
	 */
	private void expandableListProcesForPageSwich() {
		if (listSelectedLeagueNmae.isEmpty()) {
			updateListExpandadble(listContent, true);
		} else {
			updateListExpandadble(listContent, false);
		}
		sportteryFootballExpandableListAdapter.notifyDataSetChanged();
		expandableListViewContent.setSelectedGroup(0);
		updateTitleView(0);
		for (int i = 0; i < listGroup.size(); i++)
			expandableListViewContent.expandGroup(i);
	}

	private void threadAgainst() {
		/** 推荐赛事加入 */
		isUpdateAgainstList = true;
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					/** 获取全部对阵信息接口 */
					boolean b = sportteryFootballNoFilterAgainstInfoCommunicater.isGetInfoSucceed(type, -1);
					if (b) {
						Message msg = handlerChoosePage.obtainMessage(GETAGAINSTLIST_SUCCEED, null);
						handlerChoosePage.sendMessage(msg);
					} else {
						Message msg = handlerChoosePage.obtainMessage(GETAGAINSTLIST_FAIL, null);
						handlerChoosePage.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChoosePage.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerChoosePage.sendMessage(msg);
				}
			}
		}.start();
	}

	private void threadRecommendAgainst() {
		/** 推荐赛事加入 */
		isUpdateAgainstList = true;
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					/** 获取全部对阵信息接口 */
					boolean b = sportteryFootballRecommendAgainstInfoCommunicater.isGetInfoSucceed(type);
					if (b) {
						Message msg = handlerChoosePage.obtainMessage(GETRECOMMENDAGAINSTLIST_SUCCEED, null);
						handlerChoosePage.sendMessage(msg);
					} else {
						Message msg = handlerChoosePage.obtainMessage(GETRECOMMENDAGAINSTLIST_FAIL, null);
						handlerChoosePage.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChoosePage.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerChoosePage.sendMessage(msg);
				}
			}
		}.start();
	}

	private boolean canCommunicate() {
		boolean b;
		if (!isGettingTeamList && !isUpdateAgainstList) {
			b = true;
		} else {
			b = false;
		}
		return b;
	}

	private void threadGetTeamList() {
		isGettingTeamList = true;
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean b = sportteryFootballGetTeamListCommunicater.isGetTeamListSucceed();
					if (b) {
						Message msg = handlerChoosePage.obtainMessage(GETTEAMLIST_SUCCEED, null);
						handlerChoosePage.sendMessage(msg);
					} else {
						Message msg = handlerChoosePage.obtainMessage(GETTEAMLIST_FAIL, null);
						handlerChoosePage.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChoosePage.obtainMessage(GETTEAMLIST_CONNECTION_FAIL, null);
					handlerChoosePage.sendMessage(msg);
				}
			}
		}.start();
	}

	private void updateListContent() {
		int titleTaget = 0; // title在列表中的位置
		int itemCount = 1; // 相同title下元素个数
		if (listInfo.size() > 0) {
			listContent.clear();
			listContent.add(tableInListContentIsTitle(0, itemCount));

			listContent.add(tableInListContentNotTitle(0));

			for (int i = 1; i < listInfo.size(); i++) {
				if (listInfo.get(i).getMatchNumDate().equals(listInfo.get(i - 1).getMatchNumDate())) {
					listContent.add(tableInListContentNotTitle(i));
					itemCount++;
					listContent.get(titleTaget).put(ITEM_COUNT, itemCount);

				} else {
					EDebug.o(SportteryFootballChooseList.this, "date is " + listInfo.get(i).getMatchNumDate());
					EDebug.o(SportteryFootballChooseList.this, "date before is " + listInfo.get(i - 1).getMatchNumDate());
					itemCount = 1;
					titleTaget = listContent.size();
					EDebug.o(SportteryFootballChooseList.this, "title taget is " + titleTaget);
					listContent.add(tableInListContentIsTitle(i, itemCount));
					listContent.add(tableInListContentNotTitle(i));
				}
			}
		} else {
			listContent.clear();
		}
	}

	private String POSITION = "position";

	private void updateListExpandadble(ArrayList<Hashtable<String, Object>> list, boolean isAll) {
		againstTeamListLocalFilter = new AgainstTeamListLocalFilter(list);
		againstTeamListLocalFilter.getResult(rangqiuType, peilvType, listSelectedLeagueNmae, isAll);
		listChild.clear();
		listGroup.clear();
		listChildView.clear();
		for (int i = 0; i < list.size(); i++) {
			if ((Boolean) list.get(i).get(IS_TITLE)) {
				listGroup.add(list.get(i));
				ArrayList<Hashtable<String, Object>> listChild_ = new ArrayList<Hashtable<String, Object>>();
				listChild.add(listChild_);
				ArrayList<View> listChildView_ = new ArrayList<View>();
				listChildView.add(listChildView_);
			} else if ((Boolean) list.get(i).get(IS_SHOW)) {
				list.get(i).put(POSITION, i);
				listChild.get(listChild.size() - 1).add(list.get(i));
			} else {
				list.get(i).put(POSITION, i);
			}
		}
		for (int i = 0; i < listGroup.size(); i++) {
			listGroup.get(i).put(ITEM_COUNT, listChild.get(i).size());
		}
	}

	private static final String IS_DAN = "is_dan";
	private static final String IS_SHOW = "is_show";

	private Hashtable<String, Object> tableInListContentNotTitle(int position) {
		Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
		hashtable.put(INFO, listInfo.get(position));
		hashtable.put(IS_TITLE, false);
		hashtable.put(W, false);
		hashtable.put(D, false);
		hashtable.put(L, false);
		hashtable.put(IS_ACTIVE, false);
		hashtable.put(IS_DAN, false);
		hashtable.put(IS_SHOW, true);
		return hashtable;
	}

	private Hashtable<String, Object> tableInListContentIsTitle(int position, int itemCount) {
		Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
		hashtable.put(ITEM_COUNT, itemCount);
		hashtable.put(IS_TITLE, true);
		hashtable.put(DATE, listInfo.get(position).getMatchNumDate());
		hashtable.put(DAY_OF_WEEK, listInfo.get(position).getDayOfWeek());
		hashtable.put(IS_ACTIVE, false);
		hashtable.put(IS_DAN, false);
		hashtable.put(IS_SHOW, true);
		return hashtable;
	}

	@Override
	public void onClick(View v) {

		if (isGettingTeamList) {
			Toast.makeText(context, "信息加载中...", Toast.LENGTH_SHORT).show();
		} else {
			switch (v.getId()) {

			case R.id.sportteryfootballchooseact_btn_refresh:
				Message msg = handlerChoosePage.obtainMessage(DO_REFRESH, null);
				handlerChoosePage.sendMessage(msg);
				break;

			case R.id.sportteryfootballchooseact_btn_clean:
				cleanListView();
				break;

			case R.id.sportteryfootballchooseact_btn_submit:
				if (getActiveTeamCount() < getLimitEventCount()) {
					ViewerUtil.showTipDialog(context, "最少选择" + getLimitEventCount() + "场比赛", false);
				} else if (UserInfo.getUserInfo().getUserSession().length() < 3) {
					Intent intent = new Intent();
					intent.setClass(context, LoginAct.class);
					((Activity) context).startActivity(intent);
				} else {
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getLotterList().clear();
					SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getLotterList().addAll(listContent);
					((Activity) context).setContentView(buyLotteryPage.getView());
				}
				break;

			default:
				break;
			}
		}
	}

	private int getLimitEventCount() {
		int i = 0;
		if (type == 1) {
			i = 1;
		} else if (type == 2) {
			i = 2;
		}
		return i;
	}

	private void cleanListView() {

		for (int i = 0; i < listContent.size(); i++) {
			if (!(Boolean) listContent.get(i).get(IS_TITLE)) {
				listContent.get(i).put(W, false);
				listContent.get(i).put(D, false);
				listContent.get(i).put(L, false);
				listContent.get(i).put(IS_ACTIVE, false);
				listContent.get(i).put(IS_DAN, false);
			}
		}
		updateListExpandadble(listContent, false);
		sportteryFootballExpandableListAdapter.notifyDataSetChanged();
	}

	/**
	 * @Title: sortListInfo
	 * @Description: (对返回列表进行日期排序)
	 * @param: 设定文件
	 * @return: void 返回类型
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void sortListInfo() {
		Collections.sort(listInfo, new Comparator() {
			@Override
			public int compare(Object object1, Object object2) {
				SportteryFootballAgainstInfo sa1 = (SportteryFootballAgainstInfo) object1;
				SportteryFootballAgainstInfo sa2 = (SportteryFootballAgainstInfo) object2;
				int i = (sa1.getMatchNumDate()).compareTo(sa2.getMatchNumDate());
				return i;
			}
		});

	}

	private void updateTableLottery(int position, boolean w, boolean d, boolean l, CheckBox cbx, String key, SportteryFootballAgainstInfo sportteryFootballAgainstInfo) {
		boolean isChecked = false;
		if (key.equals(W)) {
			isChecked = w;
		} else if (key.equals(D)) {
			isChecked = d;
		} else if (key.equals(L)) {
			isChecked = l;
		} else {
			EDebug.o(SportteryFootballChooseList.this, "sun your sister ...");
		}
		if (getActiveTeamCount() > getMaxTeamNum() - 1 && !(Boolean) listContent.get(position).get(IS_ACTIVE)) {
			cbx.setChecked(false);
			Toast.makeText(context, "最多选择" + getMaxTeamNum() + "场比赛", Toast.LENGTH_SHORT).show();
		} else {
			listContent.get(position).put(key, isChecked);
			listContent.get(position).put(IS_ACTIVE, true);

			if (!w && !d && !l) {
				listContent.get(position).put(IS_ACTIVE, false);
			}
		}
	}

	private int getActiveTeamCount() {
		int activeTeamCount = 0;
		for (int i = 0; i < listContent.size(); i++) {
			if ((Boolean) listContent.get(i).get("is_active")) {
				activeTeamCount++;
			}
		}
		return activeTeamCount;
	}

	class TeamListAdapter extends BaseAdapter {
		private LayoutInflater listInflater;
		private boolean scroll = true;

		public TeamListAdapter(Context context) {
			this.listInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return saishiItemCheckedList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			if (scrollViewFilter != null && scroll) {
				EDebug.o(SportteryFootballChooseList.this, "adapter触发,scrollto 0");
				scrollViewFilter.scrollTo(0, 0);
				scroll = false;
			}
			if (convertView == null && alViewsList.size() < saishiItemCheckedList.size()) {
				convertView = (RelativeLayout) listInflater.inflate(R.layout.sportteryfootball_dialog_saishi_item, null);
				// }

				ImageView img = (ImageView) convertView.findViewById(R.id.sportteryfootballdialogsaishiitem_img_item);
				img.setTag(position);
				Object[] objs = { (String) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(position).get(IMAGE_ID), img };

				if (listImageCache.size() < saishiItemCheckedList.size()) {
					listImageCache.add(objs);
					EDebug.o(SportteryFootballChooseList.this, "添加item & the size is " + listImageCache.size());
					if (listImageCache.size() == saishiItemCheckedList.size()) {
						Message msg = handlerChoosePage.obtainMessage(GET_IMAGECACHE_SUCCEED, null);
						handlerChoosePage.sendMessage(msg);
					}
				}
				CheckBox cbxItem = (CheckBox) convertView.findViewById(R.id.sportteryfootballdialogsaishiitem_cbx_item);
				cbxItem.setText((String) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(position).get(SAISHI_NAME));
				cbxItem.setChecked(true);
				OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						saishiItemCheckedList.set(position, isChecked);
					}
				};
				cbxItem.setOnCheckedChangeListener(onCheckedChangeListener);
				alViewsList.add(convertView);
			}
			return alViewsList.get(position);
		}
	}

	// 判断最大场次
	private int getMaxTeamNum() {
		int i = 0;
		if (type == 1) {
			i = 8;
		} else if (type == 2) {
			i = 15;
		} else {
			EDebug.o(SportteryFootballChooseList.this, "玩法出错");
		}
		EDebug.o(SportteryFootballChooseList.this, "max team num is " + i);
		return i;
	}

	private ArrayList<ArrayList<Hashtable<String, Object>>> listChild = new ArrayList<ArrayList<Hashtable<String, Object>>>();
	private ArrayList<Hashtable<String, Object>> listGroup = new ArrayList<Hashtable<String, Object>>();
	private ArrayList<ArrayList<View>> listChildView = new ArrayList<ArrayList<View>>();

	class SportteryFootballExpandableListAdapter extends BaseExpandableListAdapter {
		private LayoutInflater listInflater;

		public SportteryFootballExpandableListAdapter(Context context) {
			this.listInflater = LayoutInflater.from(context);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return listChild.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				EDebug.o(SportteryFootballChooseList.this, "新建view " + childPosition);
				convertView = (LinearLayout) listInflater.inflate(R.layout.sportteryfootball_choose_item, null);
			}
			final SportteryFootballAgainstInfo sportteryFootballAgainstInfo = (SportteryFootballAgainstInfo) listChild.get(groupPosition).get(childPosition).get(INFO);
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitem_tv_homeName)).setText(sportteryFootballAgainstInfo.getHomeSxName());
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitem_tv_visiterName)).setText(sportteryFootballAgainstInfo.getAwaySxName());
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitem_tv_leagueName)).setText(sportteryFootballAgainstInfo.getSimpleLeague());
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitem_tv_rangqiu)).setText(sportteryFootballAgainstInfo.getRangqiu());
			String strPtggEndTime = sportteryFootballAgainstInfo.getPtggEndTime().substring(10, 16);
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitem_tv_ptggEndTime)).setText(strPtggEndTime);

			final CheckBox cbxW = (CheckBox) convertView.findViewById(R.id.sportteryfootballchooseitem_cbx_W);
			final CheckBox cbxD = (CheckBox) convertView.findViewById(R.id.sportteryfootballchooseitem_cbx_D);
			final CheckBox cbxL = (CheckBox) convertView.findViewById(R.id.sportteryfootballchooseitem_cbx_L);

			final int position = (Integer) listChild.get(groupPosition).get(childPosition).get(POSITION);
			OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					switch (buttonView.getId()) {
					case R.id.sportteryfootballchooseitem_cbx_W:
						updateTableLottery(position, isChecked, cbxD.isChecked(), cbxL.isChecked(), cbxW, W, sportteryFootballAgainstInfo);
						break;

					case R.id.sportteryfootballchooseitem_cbx_D:
						updateTableLottery(position, cbxW.isChecked(), isChecked, cbxL.isChecked(), cbxD, D, sportteryFootballAgainstInfo);
						break;

					case R.id.sportteryfootballchooseitem_cbx_L:
						updateTableLottery(position, cbxW.isChecked(), cbxD.isChecked(), isChecked, cbxL, L, sportteryFootballAgainstInfo);
						break;

					default:
						break;
					}

				}
			};
			cbxW.setOnCheckedChangeListener(onCheckedChangeListener);
			cbxD.setOnCheckedChangeListener(onCheckedChangeListener);
			cbxL.setOnCheckedChangeListener(onCheckedChangeListener);

			cbxW.setChecked((Boolean) listChild.get(groupPosition).get(childPosition).get(W));
			cbxD.setChecked((Boolean) listChild.get(groupPosition).get(childPosition).get(D));
			cbxL.setChecked((Boolean) listChild.get(groupPosition).get(childPosition).get(L));

			cbxW.setText("胜SP" + sportteryFootballAgainstInfo.getWin());
			cbxD.setText("平SP" + sportteryFootballAgainstInfo.getDraw());
			cbxL.setText("负SP" + sportteryFootballAgainstInfo.getLost());

			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return listChild.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return listGroup.get(groupPosition);
		}

		@Override
		public int getGroupCount() {

			return listGroup.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = (LinearLayout) listInflater.inflate(R.layout.sportteryfootball_choose_item_title, null);
			}
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitemtitle_tv_date)).setText((String) listGroup.get(groupPosition).get(DATE));
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitemtitle_tv_day)).setText((String) listGroup.get(groupPosition).get(DAY_OF_WEEK));
			((TextView) convertView.findViewById(R.id.sportteryfootballchooseitemtitle_tv_mtachCount)).setText(Integer.toString((Integer) listGroup.get(groupPosition).get(ITEM_COUNT)));
			ImageView imgExpand = (ImageView) convertView.findViewById(R.id.sportteryfootballchooseitemtitle_img_expand);
			if (isExpanded) {
				imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_off_38x38);
			} else {
				imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_on_38x38);
			}
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {

			return false;
		}

		@Override
		public boolean areAllItemsEnabled() {
			return true;
		}

		public void hideGroup(int groupPos) {

		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

		ExpandableListView listView = (ExpandableListView) view;

		/**
		 * calculate point (0,0)
		 */
		int npos = view.pointToPosition(0, 0);
		long pos = listView.getExpandableListPosition(npos);
		int childPos = ExpandableListView.getPackedPositionChild(pos);
		int groupPos = ExpandableListView.getPackedPositionGroup(pos);
		if (npos != AdapterView.INVALID_POSITION) {
			if (childPos == listChild.get(groupPos).size() - 1) {

				linearTitle.setAnimation(AnimationUtils.loadAnimation(context, R.anim.fade));
				linearTitle.setVisibility(View.GONE);
			} else {
				updateTitleView(groupPos);
			}
			if (childPos == AdapterView.INVALID_POSITION) {

				updateTitleView(groupPos);

			}
		}
	}

	private void updateTitleView(final int groupPos) {
		linearTitle.setVisibility(View.VISIBLE);
		if (!listGroup.isEmpty()) {
			tvDate.setText((String) listGroup.get(groupPos).get(DATE));
			tvDay.setText((String) listGroup.get(groupPos).get(DAY_OF_WEEK));
			EDebug.o(SportteryFootballChooseList.this, "item count is " + listGroup.get(groupPos).get(ITEM_COUNT));
			tvSaishiCount.setText(Integer.toString((Integer) listGroup.get(groupPos).get(ITEM_COUNT)));
		} else {
			linearTitle.setVisibility(View.GONE);
		}
		if (expandableListViewContent.isGroupExpanded(groupPos)) {
			imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_off_38x38);
		} else {
			imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_on_38x38);
		}
		linearTitle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (expandableListViewContent.isGroupExpanded(groupPos)) {
					imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_on_38x38);
					expandableListViewContent.collapseGroup(groupPos);
					expandableListViewContent.setSelectedGroup(groupPos);
				} else {
					imgExpand.setBackgroundResource(R.drawable.button_default_pickteam_off_38x38);
					expandableListViewContent.expandGroup(groupPos);
				}

			}
		});

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	private Dialog filterDialog;
	private int rangqiuType;
	private int rangqiuType_ = 0;
	private int peilvType;
	private int peilvType_ = 0;
	private ArrayList<String> listSelectedLeagueNmae = new ArrayList<String>();
	private ArrayList<RadioButton> listRbRangqiu = new ArrayList<RadioButton>();
	private ArrayList<RadioButton> listRbPeilv = new ArrayList<RadioButton>();
	private ScrollView scrollViewFilter;

	private void showDialogFilter() {
		if (filterDialog == null) {
			filterDialog = new Dialog(context, R.style.dialogNoFloating);
		}
		filterDialog.getWindow().setContentView(R.layout.sportteryfootball_filter_page);
		// rangqiuType_ = 0;
		// peilvType_ = 0;
		teamListAdapter = new TeamListAdapter(context);
		gridSaishiList = (GridView) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_grid_saishiList);
		gridSaishiList.setAdapter(teamListAdapter);
		scrollViewFilter = (ScrollView) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_scrollview_main);

		RadioButton rbRangqiuA = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_rangqiuA);
		RadioButton rbRangqiuR = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_rangqiu);
		RadioButton rbRangqiuU = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_unRangqiu);
		listRbRangqiu.clear();
		listRbRangqiu.add(rbRangqiuA);
		listRbRangqiu.add(rbRangqiuR);
		listRbRangqiu.add(rbRangqiuU);
		listRbRangqiu.get(rangqiuType).setChecked(true);

		RadioButton rbPeilvA = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_peilvA);
		RadioButton rbPeilvL = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_peilvL);
		RadioButton rbPeilvM = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_peilvM);
		RadioButton rbPeilvH = (RadioButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rb_peilvH);
		listRbPeilv.clear();
		listRbPeilv.add(rbPeilvA);
		listRbPeilv.add(rbPeilvL);
		listRbPeilv.add(rbPeilvM);
		listRbPeilv.add(rbPeilvH);
		listRbPeilv.get(peilvType).setChecked(true);

		OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (v.getId()) {
				case R.id.sportteryfootballfilterpage_btn_cancel:
					filterDialog.dismiss();
					break;

				case R.id.sportteryfootballfilterpage_btn_submit:
					EDebug.o(SportteryFootballChooseList.this, "完成按下");
					listSelectedLeagueNmae.clear();
					for (int i = 0; i < saishiItemCheckedList.size(); i++) {
						SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(i).put(IS_CHECKED, saishiItemCheckedList.get(i));
						if (saishiItemCheckedList.get(i)) {
							listSelectedLeagueNmae.add((String) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(i).get(SAISHI_NAME));
							EDebug.o(SportteryFootballChooseList.this, "筛选球队" + (String) SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getSimpleLeagueList().get(i).get(SAISHI_NAME));
						}
					}
					peilvType = peilvType_;
					rangqiuType = rangqiuType_;
					updateListExpandadble(listContent, false);
					sportteryFootballExpandableListAdapter.notifyDataSetChanged();
					filterDialog.dismiss();

					Message msgToGroup = SportteryFootballAgp.handlerSportteryFootballAgp.obtainMessage(SportteryFootballAgp.SHOW_FILTER, null);
					SportteryFootballAgp.handlerSportteryFootballAgp.sendMessage(msgToGroup);
					break;

				case R.id.sportteryfootballfilterpage_btn_quanxuan:
					EDebug.o(SportteryFootballChooseList.this, "do quanxuan !!!!!");
					for (int i = 0; i < alViewsList.size(); i++) {
						View view = alViewsList.get(i);
						CheckBox cb = (CheckBox) view.findViewById(R.id.sportteryfootballdialogsaishiitem_cbx_item);
						cb.setChecked(true);
					}
					break;

				case R.id.sportteryfootballfilterpage_btn_fanxuan:
					EDebug.o(SportteryFootballChooseList.this, "saishiItemCheckedList size is " + saishiItemCheckedList.size() + "alViewsList size is " + alViewsList.size());
					for (int i = 0; i < alViewsList.size(); i++) {
						View view = alViewsList.get(i);
						CheckBox cb = (CheckBox) view.findViewById(R.id.sportteryfootballdialogsaishiitem_cbx_item);
						boolean b = cb.isChecked();
						cb.setChecked(!b);
					}
					break;

				default:
					break;
				}
			}
		};

		android.widget.RadioGroup.OnCheckedChangeListener onCheckedChangeListenerRangqiu = new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				switch (checkedId) {
				case R.id.sportteryfootballfilterpage_rb_rangqiuA:
					rangqiuType_ = 0;
					break;

				case R.id.sportteryfootballfilterpage_rb_rangqiu:
					rangqiuType_ = 1;
					break;

				case R.id.sportteryfootballfilterpage_rb_unRangqiu:
					rangqiuType_ = 2;
					break;

				default:
					break;
				}

			}
		};
		android.widget.RadioGroup.OnCheckedChangeListener onCheckedChangeListenerPeilv = new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				switch (checkedId) {
				case R.id.sportteryfootballfilterpage_rb_peilvA:
					peilvType_ = 0;
					break;

				case R.id.sportteryfootballfilterpage_rb_peilvL:
					peilvType_ = 1;
					break;

				case R.id.sportteryfootballfilterpage_rb_peilvM:
					peilvType_ = 2;
					break;

				case R.id.sportteryfootballfilterpage_rb_peilvH:
					peilvType_ = 3;
					break;
				default:
					break;
				}

			}
		};
		ImageButton btnCancel = (ImageButton) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_btn_cancel);
		btnCancel.setOnClickListener(onClickListener);
		Button btnSubmit = (Button) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_btn_submit);
		btnSubmit.setOnClickListener(onClickListener);
		Button btnQuanxuan = (Button) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_btn_quanxuan);
		btnQuanxuan.setOnClickListener(onClickListener);
		Button btnFanxuan = (Button) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_btn_fanxuan);
		btnFanxuan.setOnClickListener(onClickListener);
		RadioGroup rgRangqiu = (RadioGroup) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rg_rangqiu);
		rgRangqiu.setOnCheckedChangeListener(onCheckedChangeListenerRangqiu);
		RadioGroup rgPeilv = (RadioGroup) filterDialog.getWindow().findViewById(R.id.sportteryfootballfilterpage_rg_peilv);
		rgPeilv.setOnCheckedChangeListener(onCheckedChangeListenerPeilv);

		filterDialog.setOnDismissListener(new Dialog.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

				if (imageListCache != null) {
					imageListCache.stop();
				}
			}
		});

		if (listImageCache.size() == saishiItemCheckedList.size()) {
			Message msg = handlerChoosePage.obtainMessage(GET_IMAGECACHE_SUCCEED, null);
			handlerChoosePage.sendMessage(msg);
		}

		filterDialog.show();
	}

	@Override
	public void setSportteryBuyLotteryPage(SportteryBuyLottery buyLotteryPage) {
		this.buyLotteryPage = buyLotteryPage;
	}
}
